<script setup lang="ts">
const props = defineProps({ markDayList: Array });
const emits = defineEmits(["chooseDateChange"]);

let weekDic = {
  1: "一",
  2: "二",
  3: "三",
  4: "四",
  5: "五",
  6: "六",
  7: "天",
};

let today = new Date();
let year = ref(today.getFullYear());
let month = ref(today.getMonth() + 1);

let choosedDate = ref("");

// 每个月最后一天的日期(下个月的第0天，这自动会被解析为本月的最后一天)
let lastDay = computed(() => new Date(year.value, month.value, 0).getDate());

// 每个月第一天的星期
let firstDayWeek = computed(() => {
  let temp = new Date(year.value, month.value - 1, 1).getDay();
  if (temp === 0) {
    temp = 7;
  }
  return temp;
});

// 每个月最后一天的星期
let lastDayWeek = computed(() => {
  let temp = new Date(year.value, month.value, 0).getDay();
  if (temp === 0) {
    temp = 7;
  }
  return temp;
});

// 是否为标注的日期
function ifMarked(day) {
  let my_date = [year.value, month.value, day].join("-");

  return props.markDayList.includes(my_date);
}

function chooseDayChange(day) {
  choosedDate.value = [year.value, month.value, day].join("-");
  emits("chooseDateChange", choosedDate.value);
}

function lastMonth() {
  month.value--;

  if (month.value < 1) {
    month.value = 12;
    year.value--;
  }
}

function nextMonth() {
  month.value++;

  if (month.value > 12) {
    month.value = 1;
    year.value++;
  }
}
</script>

<template>
  <div>
    <div class="calenderBox">
      <div class="dateRow">
        <div class="yearMonthLabel">{{ year }} 年 {{ month }} 月</div>
        <div class="buttonBox pointer">
          <div @click="lastMonth">&#x25B2;</div>
          <div @click="nextMonth">&#x25BC;</div>
        </div>
      </div>

      <div class="calenderitem weekItem" v-for="item in 7" :key="item">
        {{ weekDic[item] }}
      </div>

      <div
        class="calenderitem"
        v-for="item in firstDayWeek - 1"
        :key="item"
      ></div>

      <div
        class="calenderitem pointer"
        v-for="item in lastDay || []"
        :key="item"
        :class="{
          chooseMark: [year, month, item].join('-') === choosedDate,
          todayMark:
            year === today.getFullYear() &&
            month === today.getMonth() + 1 &&
            item == today.getDate(),
        }"
        @click="chooseDayChange(item)"
      >
        <div>
          {{ item }}
        </div>
        <div v-if="ifMarked(item)" class="markPoint">.</div>
      </div>

      <div
        class="calenderitem"
        v-for="item in 7 - lastDayWeek"
        :key="item"
      ></div>
    </div>
  </div>
</template>

<style scoped>
.calenderBox {
  max-width: 300px;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  box-shadow: 0px 4px 8px 0px rgba(0, 0, 0, 0.2);
  border-radius: 10px;
}

.calenderitem {
  position: relative;
  text-align: center;
  padding: 6px;
  width: 40px;
  height: 40px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  box-sizing: border-box;
  margin-bottom: 6px;
}

.dateRow {
  display: flex;
  padding: 10px;
  width: 100%;
  justify-content: space-between;
}
.pointer {
  cursor: pointer;
}
.buttonBox {
  width: 40px;
  display: flex;
  justify-content: space-between;
  user-select: none;
}
.todayMark {
  background-color: rgb(0, 103, 192);
  color: white;
  border-radius: 50%;
}
.markPoint {
  font-size: 40px;
  font-weight: bold;
  bottom: -8px;
  /* 绝对定位元素水平居中 */
  position: absolute;
  left: 50%;
  transform: translate(-50%);
  color: green;
}
/* 被选中的日期 */
.chooseMark {
  border: 2px solid red;
  color: red;
  border-radius: 50%;
}

.weekItem {
  font-weight: bold;
}
</style>
